﻿using System;
using System.Threading.Tasks;
using BookSleeve;
using RedisDemos.Extensions;

namespace RedisDemos.Scenarios
{
    public class Cache : Scenario
    {
        private readonly Int32 expirySeconds = 5;

        protected override async Task Perform(RedisConnection conn)
        {
            // SET key value 
            await conn.Strings.Set(Db, "page:/mail/inbox", "<!DOCTYPE html><html>...</html>", expirySeconds);

            // GET key
            var cachedPage = await conn.Strings.Get(Db, "page:/mail/inbox");
            Print(cachedPage);

            "Wait {0} secs before retry".LogInfo(expirySeconds + 1);
            await Task.Delay(TimeSpan.FromSeconds(expirySeconds + 1));

            // GET key
            cachedPage = await conn.Strings.Get(Db, "page:/mail/inbox");
            Print(cachedPage);
        }

        private static void Print(Byte[] cachedPage)
        {
            "cache for key \"page:/mail/inbox\" {0}".LogInfo(cachedPage == null ? "invalidated" : cachedPage.AsString());
        }
    }
}
